package org.dtrd.modules.evaluation.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.EvaluationInfo;
import org.dtrd.modules.evaluation.entity.bean.PatientEvaluationInfo;
import org.dtrd.modules.evaluation.entity.dto.EvaluationBatchDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntRdEvaluation;
import org.dtrd.modules.evaluation.service.IDtrdEntRdEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * @author qr
 * @date 2021/11/9 7:13 PM
 */
@Api(tags = "量表评估")
@RestController
@RequestMapping("/evaluation")
public class EvaluationInfoController extends BaseController {

    @Autowired
    private IDtrdEntRdEvaluationService evaluationService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "获取用户填写过的最新的量表数据", response = PatientEvaluationInfo.class)
    @GetMapping("/get/latest")
    public Result<?> getLatestPatientEvaluationInfo(@RequestParam Integer patientId) {
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        PatientEvaluationInfo info = evaluationService.getLatestPatientEvaluationInfo(patientId);
        return Result.OK(info);
    }

    @ApiOperation(value = "获取量表信息列表", response = EvaluationInfo.class)
    @GetMapping("/list")
    public Result<?> getEvaluationInfoList(@RequestParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)){
            return error(STATE_PARAMS_MISSING);
        }
        List<EvaluationInfo> evaluationInfoList = evaluationService.getEvaluationInfoList(patientId);
        String logContent = String.format("获取量表信息列表，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(evaluationInfoList);
    }

    @ApiOperation("更新或修改量表信息")
    @PostMapping("/edit")
    public Result<?> saveOrUpdateEvaluationInfo(@RequestBody EvaluationInfo info){
        if (StrUtil.isBlank(info.getEvaluationName())){
            return error(STATE_PARAMS_MISSING);
        }
        DtrdEntRdEvaluation evaluation = new DtrdEntRdEvaluation();
        if (info.getDataId() == null){
            evaluation = evaluation.init();
        }
        evaluation = evaluation.parseFromDTO(info);
        boolean result = evaluationService.saveOrUpdate(evaluation);
        String logContent = String.format("更新或修改量表信息，编辑量表id：%d, 量表名称: %s", info.getDataId(), info.getEvaluationName());
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除量表信息")
    @DeleteMapping("/remove")
    public Result<?> removeEvaluationInfoByIds(@RequestBody EvaluationBatchDTO dto){
        Set<Integer> evaluationIds = dto.getEvaluationIds();
        if (evaluationIds == null || evaluationIds.isEmpty()) {
            return error(STATE_PARAMS_MISSING);
        }
        evaluationService.removeByIds(evaluationIds);
        String logContent = String.format("删除量表信息，删除记录id集合：%s", dto.getEvaluationIds());
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return Result.OK("删除成功");
    }

}
